<?php
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller{
    public function Goods(){
        $username = cookie('username');
        $this->assign('username',$username);

        $ajax = I("get.");
        $where=array();
        if($ajax[cName]){
            $where['cName']=array('LIKE','%'.$ajax[cName].'%');
        }
        $User = M('Pro'); // 实例化User对象
        $count      = $User->join('imooc_cate on imooc_cate.id=imooc_pro.cId')
            ->where($where)->count();
        // 查询满足要求的总记录数
        $Page       = new \Think\Page($count,5);
        // 实例化分页类 传入总记录数和每页显示的记录数(5)
        $Page->setConfig('prev','上一页');
        $Page->setConfig('next','下一页');
        $show       = $Page->show();
        // 分页显示输出
        // 进行分页数据查询 注意limit方法的参数要使用Page类的属性
        $list = $User->join('imooc_cate on imooc_cate.id=imooc_pro.cId')
            ->where($where)
            ->field('imooc_pro.id,imooc_pro.pName,imooc_pro.pubTime,imooc_pro.pNum,imooc_cate.cName')
            ->limit($Page->firstRow.','.$Page->listRows)->select();
        $this->assign('list',$list);
        // 赋值数据集
        $this->assign('page',$show);
        // 赋值分页输出
        $this->display();
    }
    public function Del(){
        $ajax = I("post.");
        $id = rtrim($ajax['id'],",");
        $Del = M('Pro');
        $rs = $Del->where('id="'.$id.'"')->delete();
        if($rs === false){
            $this->ajaxReturn(array('flag'=>0));
        }else{
            $this->ajaxReturn(array('flag'=>1));
        }
    }
    public function SaveGoods(){
        $username = cookie('username');
        $this->assign('username',$username);

        $getId = I('get.');
        $id = $getId['id'];
        $this->assign('id',$id);
        $SelectUN = M('Pro');
        $rs = $SelectUN->join('imooc_cate on imooc_cate.id=imooc_pro.cId')
            ->where('imooc_pro.id="'.$id.'"')->select();
        $pName = $rs[0]['pName'];
        $this->assign('pName',$pName);
        $pubTime = $rs[0]['pubTime'];
        $this->assign('pubTime',$pubTime);
        $cId = $rs[0]['cId'];
        $this->assign('cId',$cId);
        $pNum = $rs[0]['pNum'];
        $this->assign('pNum',$pNum);
        $this->display();
    }
    public function doSaveGoods(){
        $post = $_POST;
        $id = $post['id'];
        $Save = M('Pro');
        $rs = $Save->join('imooc_cate on imooc_cate.id=imooc_pro.cId')
            ->where('imooc_pro.id="'.$id.'"')->save($post);
        if($rs){
            $this->success("修改成功！",U('Admin/Goods/Goods'));
        }else{
            $this->error("修改失败！");
        }
    }
    public function addGoods(){
        $username = cookie('username');
        $this->assign('username',$username);

        $User = M('Cate');
        $list = $User->select();
        $this->assign('list',$list);
        $this->display();
    }
    public function doAddGoods(){
        $post = $_POST;

        foreach($post as $key=>$value){
            if($value==null){
                $this->error("商品信息不能为空！");
            }
        }

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Public/';
        $upload->autoSub       = false;
        $upload->savePath  =      './Uploads/'; // 设置附件上传目录
        $upload->md5 = false;
        $upload->sha1 = false;
        $upload->saveRule= '';
        $upload->hash=false;
        $upload->saveName='';

        // 上传文件
        $info   =   $upload->upload();
        $imgName = $info['photo']['name'];

        $pImg = "./Public/Uploads/$imgName";
        $map = $post;
        $map['pImg'] = $pImg;
        $pSn = $post['pSn'];
        $pName = $post['pName'];
        $AddPro = M('Pro');
        $rs = $AddPro->where('pSn="'.$pSn.'"')->select();
        $rr = $AddPro->where('pName="'.$pName.'"')->select();
        if($rs || $rr){
            $this->error("商品名称或商品货号已存在！");
        }else{
            $res = $AddPro->data($map)->add();
            if($res){
                $this->success("添加成功！");
            }else{
                $this->error("添加失败！");
            }
        }
    }
}

